package com.coolgor.axnut.service.lordsee.impl;

import com.coolgor.axnut.dao.lordsee.ScanIPResultDao;
import com.coolgor.axnut.dto.lordsee.ScanIPResultExecution;
import com.coolgor.axnut.entity.lordsee.ScanIPResult;
import com.coolgor.axnut.enums.lordsee.ScanIPResultOperationStateEnum;
import com.coolgor.axnut.exceptions.lordsee.ScanIPResultOperationException;
import com.coolgor.axnut.service.lordsee.ScanIPResultService;
import com.coolgor.axnut.util.PageCalculator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ScanIPResultServiceImpl implements ScanIPResultService {
    private static Logger log = LoggerFactory.getLogger(ScanIPResultServiceImpl.class);

    @Autowired
    ScanIPResultDao scanIPResultDao;

    @Override
    public ScanIPResult getScanIPResult(Long recordID) {
        return scanIPResultDao.queryScanIPResultById(recordID);
    }

    @Override
    public ScanIPResultExecution getScanIPResultList(ScanIPResult scanIPResultCondition, int pageIndex, int pageSize) {
        //将页码转换成行码
        int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
        //依据查询条件，调用dao层返回相关的查询结果列表
        List<ScanIPResult> list = scanIPResultDao.queryScanIPResultList(scanIPResultCondition, rowIndex, pageSize);
        //依据相同的查询条件，返回查询结果总数
        int count = scanIPResultDao.queryScanIPResultCount(scanIPResultCondition);
        ScanIPResultExecution result = new ScanIPResultExecution();
        if (null != list) {
            result.setState(ScanIPResultOperationStateEnum.SUCCESS.getState());
            result.setStateInfo(ScanIPResultOperationStateEnum.SUCCESS.getStateInfo());
            result.setList(list);
            result.setCount(count);
        } else {
            result.setState(ScanIPResultOperationStateEnum.INNER_ERROR.getState());
            result.setStateInfo(ScanIPResultOperationStateEnum.INNER_ERROR.getStateInfo());
        }
        return result;
    }

    @Override
    @Transactional
    public ScanIPResultExecution addScanIPResult(ScanIPResult scanIPResult)
            throws ScanIPResultOperationException{
        // 空值判断
        if ( null == scanIPResult) {
            return new ScanIPResultExecution(ScanIPResultOperationStateEnum.NULL);
        }
        int effectedNum;
        try {
            // 添加信息
            effectedNum = scanIPResultDao.insertScanIPResult(scanIPResult);
            if (effectedNum <= 0) {
                throw new ScanIPResultOperationException("扫描IP结果插入失败");
            }
        } catch (Exception e) {
            throw new ScanIPResultOperationException("Add ScanIPResult error:" + e.getMessage());
        }
        return new ScanIPResultExecution(ScanIPResultOperationStateEnum.SUCCESS,scanIPResult);
    }

    @Override
    @Transactional
    public ScanIPResultExecution modifyScanIPResult(ScanIPResult scanIPResult)
            throws ScanIPResultOperationException{
        // 空值判断
        if (scanIPResult == null) {
            return new ScanIPResultExecution(ScanIPResultOperationStateEnum.NULL);
        }
        int effectedNum;
        try {
            // 更新信息
            effectedNum = scanIPResultDao.updateScanIPResult(scanIPResult);
            if (effectedNum <= 0) {
                throw new ScanIPResultOperationException("扫描IP结果更新失败");
            }
        } catch (Exception e) {
            throw new ScanIPResultOperationException("Modify ScanIPResult error:" + e.getMessage());
        }
        return new ScanIPResultExecution(ScanIPResultOperationStateEnum.SUCCESS,scanIPResult);
    }

    @Override
    @Transactional
    public ScanIPResultExecution removeScanIPResult(ScanIPResult scanIPResult)
            throws ScanIPResultOperationException{
        // 空值判断
        if (scanIPResult == null) {
            return new ScanIPResultExecution(ScanIPResultOperationStateEnum.NULL);
        }
        int effectedNum;
        try {
            // 执行删除操作
            effectedNum = scanIPResultDao.deleteScanIPResult(scanIPResult);
            if (effectedNum <= 0) {
                throw new ScanIPResultOperationException("扫描IP结果删除失败");
            }
        } catch (Exception e) {
            throw new ScanIPResultOperationException("Remove ScanIPResult error:" + e.getMessage());
        }
        return new ScanIPResultExecution(ScanIPResultOperationStateEnum.SUCCESS,scanIPResult);
    }

    @Override
    @Transactional
    public ScanIPResultExecution batchRemoveScanIPResult(List<ScanIPResult> list)
            throws ScanIPResultOperationException{
        // 空值判断
        if (list == null) {
            return new ScanIPResultExecution(ScanIPResultOperationStateEnum.NULL);
        }
        List<Long> recordIDList = new ArrayList<Long>();
        for (int i=0; i<list.size(); i++) {
            recordIDList.add(list.get(i).getRecordID());
        }
        int effectedNum;
        try {
            // 执行删除操作
            effectedNum = scanIPResultDao.batchDeleteScanIPResult(recordIDList);
            if (effectedNum <= 0) {
                throw new ScanIPResultOperationException("扫描IP结果批量删除失败");
            }
        } catch (Exception e) {
            throw new ScanIPResultOperationException("Batch Delete ScanIPResult error:" + e.getMessage());
        }
        ScanIPResultExecution result = new ScanIPResultExecution(ScanIPResultOperationStateEnum.SUCCESS);
        result.setCount(effectedNum);
        return result;
    }
}
